import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import lazyLoad from 'vue-lazyload';
import VueCookie from 'vue-cookie';
import ElementUI from 'element-ui';
import infiniteScroll from 'vue-infinite-scroll'
import 'element-ui/lib/theme-chalk/index.css';
import 'babel-polyfill';

// 引入样式文件
import "./assets/scss/reset.scss"
import "./assets/scss/config.scss"
import './assets/scss/element-variables.scss' //引入修改element主题色的样式

import api from './request/api'

Vue.prototype.$api = api
    // 安装插件
Vue.use(lazyLoad, {
    loading: '/imgs/loading-svg/loading-cubes.svg',
});
Vue.use(VueCookie);
Vue.use(ElementUI);
Vue.use(infiniteScroll)

// 是否开启生产版本提示 默认为不在控制台进行提示
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
    if (to.meta && to.meta.requireAuth) {
        let user = Object.keys(store.state.user).length > 0 ? store.state.user : JSON.parse(sessionStorage.getItem('pc')).user
        if (Object.keys(user).length > 0) {
            next()
        } else {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        }
    } else {
        next()
    }
    // if (to.name === 'login') {
    //   next()
    // } else {
    //   let user = JSON.parse(sessionStorage.getItem('pc')) ? JSON.parse(sessionStorage.getItem('pc')) : store.state
    //   if (Object.keys(user.user).length > 0) {
    //     next()
    //   } else {
    //     next({
    //       path: '/login',
    //       query: {
    //         redirect: to.fullPath
    //       }
    //     })
    //   }
    // }
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')